package com.jpcc.vw.onlineRadio.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;
import android.text.TextUtils;

import com.cns.android.account.CnsAccountInfo;
import com.cns.android.authentication.AppTokenInfo;
import com.cns.android.hmi.HMIManager;
import com.google.gson.Gson;
import com.jpcc.vw.onlineRadio.application.MusicApplication;
import com.jpcc.vw.onlineRadio.model.bean.CnsAccountInfoSelf;
import com.jpcc.vw.onlineRadio.model.bean.RecentAlbumRadioTrackBean;


/**
 * Preference操作工具类
 * Created by liukanshan on 2017/1/1.
 */

public class PreferenceUtils {

    public static boolean GetAudioFocusStatus() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getBoolean("focus_status", false);
    }

    public static void SaveAudioFocusStatus(boolean focused) {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("focus_status", focused);
        editor.commit();
    }


    public static void SaveLastMode(RecentAlbumRadioTrackBean lastMode) {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences(MusicApplication.GetUserId() + "", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("last_mode_json", new Gson().toJson(lastMode));
        editor.commit();
    }


    public static String GetUserLastMode() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences(MusicApplication.GetUserId() + "", Context.MODE_PRIVATE);
        return preferences.getString("last_mode_json", "");
    }


    public static void SaveRadioPlayingStatus(boolean playing) {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("playing_status", playing);
        editor.commit();
    }


    public static boolean GetRadioPlayingStatus() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getBoolean("playing_status", false);
    }


    public static void SaveFrontStatus() {
        int hmiStatus = HMIManager.HMI_IN_LINUX_SYSTEM;
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        try {
            hmiStatus = HMIManager.getInstance(MusicApplication.GetInstance()).getCurrentHMI();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        if (hmiStatus == HMIManager.HMI_IN_LINUX_SYSTEM) {
            editor.putBoolean("front_status", false);
        } else {
            if (ProcessInfoUtils.isForeground(MusicApplication.GetInstance())) {
                editor.putBoolean("front_status", true);
            } else {
                editor.putBoolean("front_status", false);
            }
        }
        editor.commit();
    }


    public static boolean GetFrontStatus() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getBoolean("front_status", false);
    }


    public static void SaveLastJson(String json) {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("last_playing_json", json);
        editor.commit();
    }


    public static String GetLastPlayingJson() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("last_playing_json", "");
    }

    public static void SaveSearchKeywords(String keyWords) {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("search_key_words", keyWords);
        editor.commit();
    }

    public static String GetSearchKeywords() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("search_key_words", "");
    }

    public static void SaveTokenInfo(AppTokenInfo info) {
        if (info != null) {
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            if((!TextUtils.isEmpty(info.getUrl()))&&(!info.getUrl().endsWith("/"))){
                info.setUrl(info.getUrl()+"/");
            }
            editor.putString("token_info", new Gson().toJson(info));
            editor.commit();
        }
        else{
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("token_info", "");
            editor.commit();
        }
    }


    public static void SaveVinNum(String vinNum){
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("vinCode", vinNum);
        editor.commit();
    }

    public static String GetVinNum(){
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("vinCode", "");
    }

    public static String GetTokenInfo() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("token_info", "");
    }


    public static void SaveAccountInfo(CnsAccountInfo info) {
        if (info != null) {
            CnsAccountInfoSelf self=new CnsAccountInfoSelf();
            self.setId(info.getId());
            self.setName(info.getName());
            self.setType(info.getType());
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("account_info", new Gson().toJson(self));
            editor.commit();
        }
        else{
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("account_info", "");
            editor.commit();
        }
    }

    public static String GetAccountInfo() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("account_info", "");
    }



    public static void SaveDeviceId(String deviceId) {
        if (!TextUtils.isEmpty(deviceId)) {
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("deviceId", deviceId);
            editor.commit();
        }
        else{
            SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("deviceId", "");
            editor.commit();
        }
    }

    public static String GetDeviceId() {
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getString("deviceId", "");
    }


    public static void SaveVIPStatus(boolean isVip){
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("IsVip", isVip);
        editor.commit();
    }


    public static boolean GetVIPStatus(){
        SharedPreferences preferences = MusicApplication.GetInstance().getSharedPreferences("radio_info", Context.MODE_PRIVATE);
        return preferences.getBoolean("IsVip", false);
    }
}
